package com.woniu108.product.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commos.dto.StoreProductDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.product.form.InsertStoreProductForm;
import com.woniu108.product.form.OutStorageForm;
import com.woniu108.product.form.PutStorageForm;
import com.woniu108.product.param.DeleteStoreProductParam;
import com.woniu108.product.param.InsertStoreProductParam;
import com.woniu108.product.param.OutStorageParam;
import com.woniu108.product.param.PutStorageParam;
import com.woniu108.product.service.StoreProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 门店库存表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/storeProduct")
@Api(tags = "门店商品库存接口")
public class StoreProductController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private StoreProductService storeProductService;

    /**
     * 查询门店库存
     *
     * @return 门店库存信息
     */
    @GetMapping("/findMyStoreProduct.st")
    @ApiOperation("查询门店库存")
    public Result<List<StoreProductDto>> findMyStoreProduct(@RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        List<StoreProductDto> list = storeProductService.findMyStoreProduct(storeAdminId);
        return Result.newListInstance(StoreProductDto.class).setData(list).setMsg("查询门店库存成功");
    }

    /**
     * 查单个商品
     * @param spInfoId 门店商品编号
     * @return  StoreProductDto
     */
    @GetMapping("/findOneStoreProductById.c")
    @ApiOperation("查单个商品")
    @ApiImplicitParam(value = "spInfoId", name = "门店商品编号")
    public Result<StoreProductDto> findOneStoreProductById(Integer spInfoId) {
        StoreProductDto dto = storeProductService.findOneStoreProductById(spInfoId);
        return Result.newInstance(StoreProductDto.class).setData(dto).setMsg("查询门店库存成功");

    }

    /**
     * 新增门店库存商品
     *
     * @param form 商品种类编号,商品品牌编号,商品详细名称,库存,门店商品描述,门店商品图片
     * @return boolean值
     * @throws Exception 文件处理报错
     */
    @PostMapping("/insertStoreProduct.st")
    @ApiOperation("新增门店库存商品")
    public Result<Boolean> insertStoreProduct(InsertStoreProductForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        InsertStoreProductParam param = BeanUtil.toBean(form, InsertStoreProductParam.class);
        param.setStoreAdminId(storeAdminId);
        Boolean bool = storeProductService.insertStoreProduct(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增门店库存商品成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增门店库存商品失败").setData(false).setCode(500);
    }

    /**
     * 商品入库
     *
     * @param form 门店商品编号 入库数量
     * @return boolean值
     */
    @PostMapping("/putStorage.st")
    @ApiOperation("商品入库")
    public Result<Boolean> putStorage(@RequestBody PutStorageForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        PutStorageParam param = BeanUtil.toBean(form, PutStorageParam.class);
        param.setStoreAdminId(storeAdminId);
        Boolean bool = storeProductService.putStorage(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("商品入库成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("商品入库失败").setData(false).setCode(500);
    }

    /**
     * 商品入库
     *
     * @param form 门店商品编号 入库数量
     * @return boolean值
     */
    @PostMapping("/putStorage.c")
    @ApiOperation("商品入库")
    public Result<Boolean> putStorageTwo(@RequestBody PutStorageForm form, @RequestHeader String token) {
     PutStorageParam param = BeanUtil.toBean(form, PutStorageParam.class);
        Boolean bool = storeProductService.putStorage(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("商品入库成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("商品入库失败").setData(false).setCode(500);
    }
    /**
     * 商品出库
     *
     * @param form 门店商品编号 入库数量
     * @return boolean值
     */
    @PostMapping("/outStorage.st")
    @ApiOperation("商品出库")
    public Result<Boolean> outStorage(@RequestBody OutStorageForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        OutStorageParam param = BeanUtil.toBean(form, OutStorageParam.class);
        param.setStoreAdminId(storeAdminId);
        Boolean bool = storeProductService.outStorage(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("商品出库成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("商品出库失败").setData(false).setCode(500);
    }
    /**
     * 商品出库
     *
     * @param form 门店商品编号 入库数量
     * @return boolean值
     */
    @PostMapping("/outStorage.c")
    @ApiOperation("商品出库")
    public Result<Boolean> outStorageTwo(@RequestBody OutStorageForm form, @RequestHeader String token) {
         OutStorageParam param = BeanUtil.toBean(form, OutStorageParam.class);

        Boolean bool = storeProductService.outStorage(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("商品出库成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("商品出库失败").setData(false).setCode(500);
    }
    /**
     * 删除某个商品
     *
     * @param spInfoId 门店商品编号
     * @return boolean值
     */
    @GetMapping("/deleteStoreProduct.st")
    @ApiOperation("删除某个商品")
    @ApiImplicitParam(value = "spInfoId", name = "门店商品编号")
    public Result<Boolean> deleteStoreProduct(Integer spInfoId, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        DeleteStoreProductParam param = new DeleteStoreProductParam(spInfoId, storeAdminId);
        Boolean bool = storeProductService.deleteStoreProduct(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除某个商品成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除某个商品失败").setData(false).setCode(500);
    }
}

